package org.bobdawg.app.MALforAndroid;

import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RatingBar;

class RatingPicker extends Dialog implements OnClickListener {
	
	private final static String LOG_NAME = "RatingPicker";
	
	private float currentRating;
	private RatingBar ratingBar;
	private Button cmdDone;
	private Button cmdCancel;
	
	private boolean canceled;

	public RatingPicker(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		this.setContentView(R.layout.rating_picker);
		
		currentRating = (float) 0.0;
		
		cmdDone = (Button) findViewById(R.id.buttonDone);
		cmdCancel = (Button) findViewById(R.id.buttonCancel);
		
		cmdDone.setOnClickListener(this);
		cmdCancel.setOnClickListener(this);
		
		ratingBar = (RatingBar) findViewById(R.id.ratingBar);
		ratingBar.setRating(currentRating);
		
		canceled = false;
	}
	
	public boolean wasCanceled() {
		return !(canceled);
	}
	
	public float getCurrentRating() {
		return currentRating;
	}
	
	public void onStart() {
		canceled = false;
	}
	
	public void setCurrentRating(float currentRating) {
		if ((float) 0.0 <= currentRating && currentRating <= (float) 5.0) {
			this.currentRating = currentRating;
			ratingBar.setRating(currentRating);
		} else {
			Log.i(LOG_NAME, "out of bounds");
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v == cmdDone) {
			canceled = true;
			currentRating = ratingBar.getRating();
			dismiss();
		}
		else {
			dismiss();
		}
		
	}

}
